package com.frame.platform.user;

import java.util.ArrayList;
import java.util.List;

import com.frame.common.SystemConfig;
import com.frame.database.DbOperate;
import com.frame.database.JdataBean;
import com.frame.pageform.component.tree.TreeJson;
import com.frame.pageform.component.tree.TreeNode;
import com.frame.platform.dept.Wfdept;

/**
 * @Title: 系统
 * @Description:
 * @Copyright: Copyright (c) 2014
 * @Company:
 * @author 王海锋
 * @CreatedTime:2014-2-20 下午04:37:42
 * @version 1.0
 */

public class UserTreeJsonPage extends TreeJson{

    @Override
    public Object getRoot() throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List query() throws Exception {
        // TODO Auto-generated method stub
        List trees=new ArrayList();
        DbOperate db=DbOperate.getInstance();
        List<Wfuser> ulist=db.query("select * from wfuser u,wfuserdept d where u.userid=d.userid and d.deptid=? and u.state=1 order by u.userid", new Object[]{new Integer(getNodeId())},Wfuser.class);
        for(Wfuser user:ulist){
            TreeNode node=new TreeNode("u_"+user.getId(),getNodeId(),0,user.getUsername());
            node.setIcon(SystemConfig.getInstance().getContextPath()+"/platform/images/icon_user.gif");
            trees.add(node);
        }
        List<Wfdept> dlist=db.query("select * from wfdept where pardeptid=? and deletetag=0 order by orderid", new Object[]{new Integer(getNodeId())},Wfdept.class);
        for(Wfdept dept:dlist){
            List<JdataBean> list=db.query("select count(*) cc from wfdept where pardeptid=? and deletetag=0", new Object[]{dept.getDeptid()});
            int n=Integer.parseInt(list.get(0).get("cc").toString());
            if(n<=0){
                List<JdataBean> ulist2=db.query("select count(*) cc from wfuser u,wfuserdept d where u.userid=d.userid and d.deptid=? and u.state=1 ", new Object[]{dept.getDeptid()});
                n=Integer.parseInt(ulist2.get(0).get("cc").toString());
            }
            TreeNode node=new TreeNode("d_"+dept.getId(),dept.getPardeptid()+"",n,dept.getDeptname());
            node.setIcon(SystemConfig.getInstance().getContextPath()+"/platform/images/dept.png");
            trees.add(node);
        }

        return trees;
    }

}
